﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using Technewlogic.FileXChange.Common;
using System.Collections.Generic;

namespace Technewlogic.FileXChange
{
	public partial class Default : System.Web.UI.Page
	{
		private readonly FileServiceLogic _fileServiceLogic = new FileServiceLogic();

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				string methodName = Request.QueryString[Constants.QueryStringMethod];
				if (!string.IsNullOrEmpty(methodName))
				{
					FileMethod method = FileMethod.Parse(Request.QueryString[Constants.QueryStringMethod]);

					if (method == FileMethod.Put)
					{
						// sollte niemals passieren, da dies über den Upload-Button geht
						throw new InvalidOperationException("The put method is not allowed");
					}
					else if (method == FileMethod.Get)
					{
						// Ausnahme: Wird nicht über _fileServiceLogik gemacht, wegen Response.WriteFile

						string fileName = _fileServiceLogic.UploadPath + "\\" + GetRequestFilename();
						var fi = new FileInfo(fileName);

						Response.Clear();
						Response.AddHeader("Content-Disposition", "attachment; filename=" + fi.Name);
						Response.AddHeader("Content-Length", fi.Length.ToString());
						Response.ContentType = "application/octet-stream";
						Response.WriteFile(fi.FullName);
						Response.End();
					}
					else if (method == FileMethod.Delete)
					{
						string fileName = GetRequestFilename();
						_fileServiceLogic.Delete(fileName);

						DisplayUserMessage("Die Datei " + fileName + " wurde gelöscht.");
					}
				}
			}
			
			CreateFilesList();
		}

		private string GetRequestFilename()
		{
			return Request.QueryString[Constants.QueryStringFilename];
		}

		private void CreateFilesList()
		{
			//FileNames.Clear();
			filesPlaceholder.Controls.Clear();

			var fis = new DirectoryInfo(_fileServiceLogic.UploadPath).GetFiles();
			foreach (var it in fis)
			{
				string getUrl = ResolveClientUrl("~/Default.aspx?" +
					Constants.QueryStringFilename + "=" + it.Name + "&" +
					Constants.QueryStringMethod + "=" + FileMethod.Get.Name);
				string delUrl = ResolveClientUrl("~/Default.aspx?" +
					Constants.QueryStringFilename + "=" + it.Name + "&" +
					Constants.QueryStringMethod + "=" + FileMethod.Delete.Name);

				LiteralControl link = new LiteralControl();
				link.Text = string.Format("<tr><td><a href=\"{0}\" Width=\"250\">{1}</a></td>",
					getUrl, it.Name);
				link.Text += string.Format("<td>{0}</td>",
					(it.Length / 1024).ToString() + "kB");
				link.Text += string.Format("<td>{0}</td>",
					it.LastWriteTime.ToString());
				link.Text += string.Format("<td><a href=\"{0}\" Width=\"250\">Delete</a></td></tr>",
					delUrl);
				filesPlaceholder.Controls.Add(link);
			}
		}

		protected void btnUpload_Click(object sender, EventArgs e)
		{
			var file = Request.Files[0];
			string fileName = Path.GetFileName(file.FileName);
			if (!string.IsNullOrEmpty(fileName))
			{
				byte[] buffer = new byte[file.ContentLength];
				file.InputStream.Read(buffer, 0, file.ContentLength);

				_fileServiceLogic.Post(fileName, buffer);

				DisplayUserMessage("Die Datei " + fileName + " wurde erfolgreich hochgeladen.");
			}
			else
				DisplayUserMessage("Bitte zuerst eine Datei zum Hochladen auswählen.");

			CreateFilesList();
		}

		private void DisplayUserMessage(string message)
		{
			lblUserMessage.Text = message;
			lblUserMessage.Visible = true;
		}
	}
}
